Imports System
Imports DotNetNuke.Framework.Providers
Imports DotNetNuke.Common.Utilities
Imports BigfootSQL


Namespace AboutDNN.DNNMVC


    Public Class DataProvider

        Private Const ProviderType As String = "data"

        Private _moduleQualifier As String = IIf(String.IsNullOrEmpty(App.ModuleDBObjectQualifier), _
                                                 "", _
                                                 App.ModuleDBObjectQualifier & "_")

        Private _providerConfiguration As ProviderConfiguration
        Private _connectionString As String
        Private _providerPath As String
        Private _objectQualifier As String
        Private _databaseOwner As String
        Private _upgradeConnectionString As String


        Public Sub New()
            ' Retreive the provider configuration if not in cache
            _providerConfiguration = App.Cache.GetValue(Params.Cache.ProviderConfiguration)
            If _providerConfiguration Is Nothing Then
                _providerConfiguration = ProviderConfiguration.GetProviderConfiguration(ProviderType)
                App.Cache.Add(Params.Cache.ProviderConfiguration, _providerConfiguration)
            End If

            ' Read the configuration specific information for this provider
            Dim objProvider As Provider = CType(_providerConfiguration.Providers(_providerConfiguration.DefaultProvider), Provider)

            ' Read the attributes for this provider

            'Get Connection string from web.config
            _connectionString = Config.GetConnectionString()

            If _connectionString = "" Then
                ' Use connection string specified in provider
                _connectionString = objProvider.Attributes("connectionString")
            End If

            If Convert.ToString(objProvider.Attributes("upgradeConnectionString")) <> "" Then
                _upgradeConnectionString = objProvider.Attributes("upgradeConnectionString")
            Else
                _upgradeConnectionString = _connectionString
            End If

            _providerPath = objProvider.Attributes("providerPath")

            _objectQualifier = objProvider.Attributes("objectQualifier")
            If _objectQualifier <> "" And _objectQualifier.EndsWith("_") = False Then
                _objectQualifier += "_"
            End If

            _databaseOwner = objProvider.Attributes("databaseOwner")
            If _databaseOwner <> "" And _databaseOwner.EndsWith(".") = False Then
                _databaseOwner += "."
            End If

        End Sub


        ''' <summary>
        ''' IoC Contractor. Used by the testing framework.
        ''' </summary>
        ''' <param name="CNString">The connection string</param>
        ''' <param name="Qualifier">The database object qualifier (e.g. dnn)</param>
        ''' <param name="Owner">The database object owner (e.g. dbo)</param>
        Public Sub New(ByVal CNString As String, ByVal Qualifier As String, ByVal Owner As String)
            _connectionString = CNString
            _objectQualifier = IIf(String.IsNullOrEmpty(Qualifier) = False, Qualifier & "_", "")
            _databaseOwner = Owner & "."
        End Sub

        ''' <summary>
        ''' IoC Contractor. Used by the testing framework.
        ''' </summary>
        ''' <param name="CNString">The connection string</param>
        ''' <param name="Qualifier">The database object qualifier (e.g. dnn)</param>
        ''' <param name="Owner">The database object owner (e.g. dbo)</param>
        Public Sub New(ByVal CNString As String, ByVal Qualifier As String, ByVal Owner As String, ByVal ModuleQualifier As String)
            _connectionString = CNString
            _objectQualifier = IIf(String.IsNullOrEmpty(Qualifier), "", Qualifier & "_")
            _databaseOwner = Owner & "."
            _moduleQualifier = IIf(String.IsNullOrEmpty(ModuleQualifier), "", ModuleQualifier & "_")
        End Sub


        Public ReadOnly Property ConnectionString() As String
            Get
                Return _connectionString
            End Get
        End Property

        Public ReadOnly Property ProviderPath() As String
            Get
                Return _providerPath
            End Get
        End Property

        Public ReadOnly Property ObjectQualifier() As String
            Get
                Return _objectQualifier
            End Get
        End Property

        Public ReadOnly Property DatabaseOwner() As String
            Get
                Return _databaseOwner
            End Get
        End Property

        Public ReadOnly Property ModuleQualifier() As String
            Get
                Return _moduleQualifier
            End Get
        End Property

        Public Function GetName(ByVal name As String) As String
            Return DatabaseOwner & ObjectQualifier & ModuleQualifier & name
        End Function

        Public Function GetNull(ByVal Field As Object) As Object
            Return DotNetNuke.Common.Utilities.Null.GetNull(Field, DBNull.Value)
        End Function

        Public Function SQL() As SqlHelper
            Return New SqlHelper(DatabaseOwner, ObjectQualifier, ModuleQualifier, ConnectionString)
        End Function

        Public Function UserExists(ByVal UserName As String) As Boolean
            Return SQL.SELECT("COUNT(*)").FROM("{{{users}}}").WHERE("UserName", UserName).ExecuteScalar(Of Integer)() > 0
        End Function

    End Class

End Namespace